package it.fuesi.synotools.adapters;

import android.app.Fragment;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.LinkedList;
import java.util.List;

import it.fuesi.synotools.R;
import it.fuesi.synotools.model.drawer.NavigationDrawerItem;

/**
 * Created by pascotto on 05/11/14.
 */
public class NavigationDrawerAdapter extends BaseAdapter {

    private Context context;
    private Fragment fragment;
    private LayoutInflater inflater;

    private static List<NavigationDrawerItem> data = null;

    public NavigationDrawerAdapter(LayoutInflater inflater, Context context, Fragment fragment){
        this.context=context;
        this.fragment=fragment;
        this.inflater = inflater;

        if(data == null){
            data = new LinkedList<NavigationDrawerItem>();

            String[] names = context.getResources().getStringArray(R.array.navigation_drawer_titles);

            final TypedArray imgs = context.getResources().obtainTypedArray(R.array.navigation_drawer_icons);

            for(int i=0; i<names.length; i++){
                final NavigationDrawerItem item = new NavigationDrawerItem();
                item.setName(names[i]);
                item.setIcon(imgs.getResourceId(i, -1));
                data.add(item);
            }

            imgs.recycle();
        }

    }


    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return data != null ? data.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null){
            vi = inflater.inflate(R.layout.navigation_drawer_item, null);
        }


        final NavigationDrawerItem item = data.get(position);

        final View finalView = vi;

        TextView mName = (TextView)vi.findViewById(R.id.item_title);
        ImageView mIcon = (ImageView)vi.findViewById(R.id.item_icon);

        mName.setText(item.getName());
        mIcon.setImageResource(item.getIcon());

        return finalView;
    }
}
